using MoonFramework.FSM;
using MoonFramework.Scripts.Event;
using Tool.Name;
using UnityEngine;

namespace Entity.Monster.State
{
    public class MonsterAttackState : AttackState
    {
        private Monster _monster => (Monster)_actor;
        private bool _isAttack;

        public override void Enter()
        {
            base.Enter();
            _monster.viewController.PlayAudio(MonsterAudioName.Hit);
            _actor.viewController.WeaponPoint.OnTriggerEnter(CheckHitOnTriggerStay);
        }

        protected override void CheckHitOnTriggerStay(Collider other, object[] args)
        {
            if(_isAttack) return;
            if (!other.gameObject.CompareTag("Character")) return;
            _isAttack = true;
            var character = other.GetComponent<Character>();
            _monster.viewController.PlayAudio(MonsterAudioName.Attack);
            character.Hurt(_monster.MonsterConfig.AttackRange);
        }

        protected override void StartHit()
        {
            _actor.viewController.WeaponPoint.gameObject.SetActive(true);
        }

        protected override void StopHit()
        {
            _isAttack = false;
            _actor.viewController.WeaponPoint.gameObject.SetActive(false);
        }

        protected override void AttackOver()
        {
            _actor.fsmMachine.ChangeState(nameof(WalkState));
        }

        public override void Exit()
        {
            base.Exit();
            _actor.viewController.WeaponPoint.RemoveTriggerEnter(CheckHitOnTriggerStay);
        }
    }
}